package com.ruoyi.yljf.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.yljf.domain.ActiveOther;
import com.ruoyi.yljf.domain.User;
import com.ruoyi.yljf.domain.dto.ActiveDTO;
import com.ruoyi.yljf.domain.enums.yljfEnums;
import com.ruoyi.yljf.domain.vo.ActiveUserVO;
import com.ruoyi.yljf.domain.vo.ActiveVO;
import com.ruoyi.yljf.service.IActiveOtherService;
import com.ruoyi.yljf.service.IUserService;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.yljf.mapper.ActiveMapper;
import com.ruoyi.yljf.domain.Active;
import com.ruoyi.yljf.service.IActiveService;

import javax.annotation.Resource;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 活动Service业务层处理
 * 
 * @author metaee
 * @date 2025-02-13
 */
@Service
public class ActiveServiceImpl extends ServiceImpl<ActiveMapper, Active> implements IActiveService {

	@Resource
    private ActiveMapper activeMapper;
	@Resource
	private ISysUserService sysUserService;
	@Resource
	private IActiveOtherService activeOtherService;
	@Resource
	private IUserService userService;
    
    /**
     * 查询活动列表
     * 
     * @param active 活动
     * @return 活动
     */
    @Override
    public List<Active> selectActiveList(Active active) {

    	LambdaQueryWrapper<Active> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotEmpty(active.getActiveName())) {
			queryWrapper.like(Active::getActiveName, active.getActiveName());
		}
        if (StringUtils.isNotNull(active.getStartTime())) {
			queryWrapper.eq(Active::getStartTime, active.getStartTime());
		}
        if (StringUtils.isNotNull(active.getEndTime())) {
			queryWrapper.eq(Active::getEndTime, active.getEndTime());
		}
        if (StringUtils.isNotEmpty(active.getActiveAddress())) {
			queryWrapper.eq(Active::getActiveAddress, active.getActiveAddress());
		}
        if (StringUtils.isNotEmpty(active.getUserPhone())) {
			queryWrapper.eq(Active::getUserPhone, active.getUserPhone());
		}
        if (StringUtils.isNotEmpty(active.getActiveInfo())) {
			queryWrapper.eq(Active::getActiveInfo, active.getActiveInfo());
		}
        if (StringUtils.isNotNull(active.getActivePrice())) {
			queryWrapper.eq(Active::getActivePrice, active.getActivePrice());
		}
        if (StringUtils.isNotEmpty(active.getActiveFree())) {
			queryWrapper.eq(Active::getActiveFree, active.getActiveFree());
		}
        if (StringUtils.isNotEmpty(active.getActiveType())) {
			queryWrapper.eq(Active::getActiveType, active.getActiveType());
		}
		return activeMapper.selectList(queryWrapper);
    }

	@Override
	public List<ActiveVO> queryActiveList(List<Active> list,Long userId) {
		ArrayList<ActiveVO> activeVOS = new ArrayList<>();
		List<ActiveVO> collect = list.stream().map(item -> beanProcess(item)).collect(Collectors.toList());
		//判断用户是否登录，然后判断用户当前活动是否报名
		 return collect.stream().map(i->{
			if (ObjectUtils.isNotEmpty(userId)) {
				ActiveOther user = activeOtherService.getOne(
						new LambdaQueryWrapper<>(ActiveOther.class)
								.eq(ActiveOther::getType, "user")
								.eq(ActiveOther::getOtherId, userId)
								.eq(ActiveOther::getActiveId, i.getActiveId())
				);
				if (ObjectUtils.isNotEmpty(user)) {
					i.setApply(true);
				}
			}
			i.setApply(false);
			return i;
		}).collect(Collectors.toList());
	}

	@Override
	public ActiveVO queryById(Long activeId) {
		Active active = this.activeMapper.selectById(activeId);
		ActiveVO activeVO = new ActiveVO(active);
		activeVO.setActiveFree(yljfEnums.descOfCode(activeVO.getActiveFree()));
		activeVO.setActiveType(yljfEnums.descOfCode(activeVO.getActiveType()));
		SysUser sysUser = sysUserService.selectUserById(active.getUserId());
		activeVO.setUserImg(sysUser.getAvatar());
		activeVO.setUserName(sysUser.getUserName());
		activeVO.setAcUserList(Collections.emptyList());
		return activeVO;
	}

	private ActiveVO beanProcess(Active item) {
		ActiveVO activeVO = new ActiveVO();
		BeanUtils.copyProperties(item,activeVO);
		activeVO.setActiveFree(yljfEnums.descOfCode(activeVO.getActiveFree()));
		activeVO.setActiveType(yljfEnums.descOfCode(activeVO.getActiveType()));
		SysUser sysUser = sysUserService.selectUserById(item.getUserId());
		activeVO.setUserImg(sysUser.getAvatar());
		List<ActiveOther> list = activeOtherService.list(
				new LambdaQueryWrapper<ActiveOther>()
						.eq(ActiveOther::getActiveId, item.getActiveId())
						.eq(ActiveOther::getType, yljfEnums.USER)
		);
		List<Long> ids = new ArrayList<>();
		List<User> users = new ArrayList<>();
		if (ObjectUtils.isNotEmpty(list)) {
			activeVO.setUserNum(list.size());
			for (int i = 0; i < list.size(); i++) {
				if (i<5){
					ids.add(list.get(i).getOtherId());
				}
			}
			users = userService.listByIds(ids);
		}else {
			activeVO.setUserNum(0);
		}
		List<ActiveUserVO> collect = users.stream().map(ActiveUserVO::new).collect(Collectors.toList());
		activeVO.setAcUserList(collect);
		return activeVO;
	}


}
